package backstage.competence.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import backstage.competence.pojo.Competence;
import backstage.competence.service.CompetenceService;
import backstage.staff.pojo.Staff;
import backstage.sys.action.PlatformAction;
import backstage.sys.util.Platform;
import backstage.util.pojo.Page;
import backstage.util.tool.PageUtil;

public class CompetenceAction extends PlatformAction{

	private static final long serialVersionUID = 3518833679938898354L;
	
	private List<Competence> list;
	private String jsonDoc;
	private String id;
	private String mid;
	private Page p;
	private String name;
	private String groupId;
	private String error;
	
	public String execute() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
			p = cs.getPage(10);
			p.setSortOrder("id");
			list = cs.getCompetence(p);
			return "success";
		}else{
			return "not";
		}
	}
	
	public String getTheCompetence() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
			this.p = PageUtil.getNewPage(this.p,this.p.getCount());
			this.list = cs.getCompetence(this.p);
			return "success";
		}else{
			return "not";
		}
	}
	
	public String add() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			if(null != this.name && "" != this.name && !"".equals(this.name) && null != this.groupId && "" != this.groupId && !"".equals(this.groupId)){
				CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
				Competence c = new Competence();
				c.setName(this.name);
				c.setGroupId(this.groupId);
				cs.addCompetence(c);
				this.p = PageUtil.getNewPage(this.p,this.p.getCount() + 1);
				this.list = cs.getCompetence(this.p);
				return "success";
			}else{
				this.error = "添加错误!";
				return execute();
			}
		}else{
			return "not";
		}
	}
	
	public String update() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			if(null != this.name && "" != this.name && !"".equals(this.name) && null != this.groupId && "" != this.groupId && !"".equals(this.groupId) && !"1111".equals(this.groupId)){
				CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
				Competence c = new Competence();
				c.setName(this.name);
				c.setGroupId(this.groupId);
				cs.addCompetence(c);
				this.p = PageUtil.getNewPage(this.p,this.p.getCount() + 1);
				this.list = cs.getCompetence(this.p);
				return "success";
			}else{
				this.error = "修改错误!";
				return execute();
			}
		}else{
			return "not";
		}
	}
	
	public String delete() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			if(null != this.id && "" != this.id && !"".equals(this.id)){
				CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
				Competence c = cs.getCompetence(Integer.parseInt(this.id));
				if(!"1111".equals(c.getGroupId())){
					cs.deleteCompetence(c);
					this.p = PageUtil.getNewPage(this.p,this.p.getCount() - 1);
					this.list = cs.getCompetence(this.p);
					return "success";
				}else{
					this.error = "删除错误!";
					return execute();
				}
			}else{
				this.error = "删除错误!";
				return execute();
			}
		}else{
			return "not";
		}
	}
	
	public String getMenuTree() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
			jsonDoc = cs.getMenusJson(new Integer(id));
			return "success";
		}else{
			return "not";
		}
	}

	public String updateMenuTree() throws Exception{
		HttpSession session = this.getSession();
		Staff po = (Staff)session.getAttribute("staff");
		String groupId = po.getCompetence().getGroupId();
		if(Integer.parseInt(groupId, 2) == 15){
			CompetenceService cs = (CompetenceService)Platform.getInstance().getApplication().getBean("competenceService");
			cs.updateMenus(id, mid);
			this.p = PageUtil.getNewPage(this.p,this.p.getCount());
			list = cs.getCompetence(p);
			return "success";
		}else{
			return "not";
		}
	}
	
	public List<Competence> getList() {
		return list;
	}

	public void setList(List<Competence> list) {
		this.list = list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page getP() {
		return p;
	}

	public void setP(Page p) {
		this.p = p;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getJsonDoc() {
		return jsonDoc;
	}

	public void setJsonDoc(String jsonDoc) {
		this.jsonDoc = jsonDoc;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

}
